﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DNA
{
    public partial class corel : Form
    {
        public Int32 numberofequel;
        public int kolnul = 0;
        public int kolmal = 0;
        public int kolmened = 0;
        public int kolsilno = 0;
        public string who1, who2;
        public string str_mal;
        public string str_sred;
        int nomer = 1;
       // int kore = 0;
        //int flag = 0;
        float _korel = 0;
        float min_korel = 0;
        float max_korel = 0;
        int kount = 0;

        public corel()
        {
            InitializeComponent();
            DNA dna = this.Owner as DNA;   
        }

        public void fillcomuns()
        {
            colums.Items.Clear();
            DNA dna = this.Owner as DNA;
            if (dna.flag == true)
            {
                numberofequel = dna.rows;
                for (int i = 0; i < dna.TDNA.Columns.Count; i++)
                {
                    colums.Items.Add(dna.TDNA.Columns[i].HeaderText);
                    colums.SetItemChecked(i, true);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            fillcomuns();
        }

        private void start_Click(object sender, EventArgs e)
        {
            int count = 0;
            for (int i = 0; i < colums.Items.Count; i++)
            {
                if (colums.GetItemChecked(i) == true) count++;
                if (count >= 2)
                {
                    startanalis();
                    break;
                }
            }
        }

        public void startanalis()
        {
            report.Clear();
            try
            {
                report.Text = report.Text + "Начало анализа № " + nomer.ToString() + '\n';
                nomer++;
                for (int i = 0; i < colums.Items.Count; i++)
                {
                    if (colums.GetItemCheckState(i) == CheckState.Checked)
                    {
                        for (int j = i + 1; j < colums.Items.Count; j++)
                            if (colums.GetItemCheckState(j) == CheckState.Checked)
                            {
                                analis(i, j);
                            }
                    }
                }
            }
            catch
            {
                MessageBox.Show("Нету данных!");
            }
            MessageBox.Show("Среднее значение = " + (_korel / kount).ToString() + '\n' + "Диапазон значений от " + min_korel.ToString() + " до " + max_korel.ToString());
        }

        public void analis(int first,int second)
        {
            float summA=0;
            float summB=0;
            float sumA, sumB;
            float chislitel=0;
            float dispersX = 0;
            float dispersY = 0;
            double znamenatel = 0;
            float korel = 0;
            bool happy = true;
                      
            DNA dna = this.Owner as DNA;
            int test;
            for (int i = 0; i < colums.Items.Count; i++)
            {
                if (colums.GetItemChecked(i) == true)
                {
                    for (int j = 0; j < 1; j++)
                    {
                        try
                        {
                            test = Convert.ToInt32(dna.TDNA[i, j].Value);
                        }
                        catch
                        {
                            colums.SetItemChecked(i, false);
                        }
                    }
                    if (colums.Items[i].ToString() == "Диагноз")
                    {
                        colums.SetItemChecked(i, false);
                    }
                }
            }
            report.Text = report.Text + colums.Items[first].ToString() + "---" + colums.Items[second].ToString()+'\n';
            for (int i = 0; i < numberofequel; i++)
            {
                try
                {
                    summA = summA + Convert.ToInt32(dna.TDNA[first, i].Value);
                    summB = summB + Convert.ToInt32(dna.TDNA[second, i].Value);
                }
                catch (System.Exception)
                {
                    MessageBox.Show("Данные не преобразованны!");
                    happy = false;
                    break;
                }
            }
            if (happy == true)
            {
                report.Text = report.Text + " Сумма коэф." + colums.Items[first].ToString() + '=' + summA.ToString() + '\n' + " Сумма коэф." + colums.Items[second].ToString() + '=' + summB.ToString() + '\n';
                sumA = summA / (numberofequel - 1);
                sumB = summB / (numberofequel - 1);
                report.Text = report.Text + " Средняя сумма " + colums.Items[first].ToString() + '=' + sumA.ToString() + '\n' + " Средняя сумма " + colums.Items[second].ToString() + '=' + sumB.ToString() + '\n';
                for (int k = 0; k < numberofequel - 1; k++)
                {
                    chislitel = chislitel + (Convert.ToInt32(dna.TDNA[first, k].Value) - sumA) * (Convert.ToInt32(dna.TDNA[second, k].Value) - sumB);
                    dispersX = dispersX + (Convert.ToInt32(dna.TDNA[first, k].Value) - sumA) * (Convert.ToInt32(dna.TDNA[first, k].Value) - sumA);
                    dispersY = dispersY + (Convert.ToInt32(dna.TDNA[second, k].Value) - sumB) * (Convert.ToInt32(dna.TDNA[second, k].Value) - sumB);

                }
                znamenatel = Math.Sqrt(dispersX * dispersY);
                korel = chislitel / (float)znamenatel;
                report.Text = report.Text + "Значение регрессии : " + korel.ToString() + '\n';
                report.Text = report.Text + "Ошибка составляет : " + ((1 - Math.Pow(korel, 2)) / (Math.Sqrt(numberofequel))) + '\n';
                {
                    _korel = _korel + (float)((1 - Math.Pow(korel, 2)) / (Math.Sqrt(numberofequel)));
                    if (kount == 0)
                    {
                        max_korel = (float)((1 - Math.Pow(korel, 2)) / (Math.Sqrt(numberofequel)));
                        min_korel = (float)((1 - Math.Pow(korel, 2)) / (Math.Sqrt(numberofequel)));
                    }
                    else
                    {
                        if (min_korel > (float)((1 - Math.Pow(korel, 2)) / (Math.Sqrt(numberofequel)))) min_korel = (float)((1 - Math.Pow(korel, 2)) / (Math.Sqrt(numberofequel)));
                        if (max_korel < (float)((1 - Math.Pow(korel, 2)) / (Math.Sqrt(numberofequel)))) max_korel = (float)((1 - Math.Pow(korel, 2)) / (Math.Sqrt(numberofequel)));
                    }
                    kount++;
                }
                postanal(korel, first, second);
            }
            
        }

        private void ok_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void colums_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            { startanalis(); }
        }

        public void postanal(float chislo,int firs,int secon)
        {
            if (chislo == 0)
            {
                kolnul++;
                who1 = who1 + colums.Items[firs].ToString() + '-' + colums.Items[secon].ToString() + '\n';
            }
            if (chislo >= (-0.1)&&(chislo <= (0.1)))
            {
                kolmal++;
                str_mal = str_mal + colums.Items[firs].ToString() + '-' + colums.Items[secon].ToString() + '\n';
            }
            if (((chislo > 0.1) && (chislo < 0.7)) || ((chislo < -0.1) && (chislo > -0.7)))
            {
                kolmened++;
                str_sred = str_sred + colums.Items[firs].ToString() + '-' + colums.Items[secon].ToString() + '\n';
            }
            if (chislo >= (0.7) || chislo <= (-0.7))
            {
                kolsilno++;
                who2 = who2 + colums.Items[firs].ToString() + '-' + colums.Items[secon].ToString() + '\n';
            }
        
        }

        private void postanalis_Click(object sender, EventArgs e)
        {
            korel_report kor = new korel_report();
            kor.Owner = this;
            kor.make_report();
            kor.Show();
        }
       
    }
}
